package com.fjb.controller.index;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.entity.JsonTotalResult;
import com.fjb.common.entity.PageQuery;
import com.fjb.common.entity.TotalResult;
import com.fjb.common.enums.banner.BannerStatus;
import com.fjb.common.enums.brand.BrandStatus;
import com.fjb.common.enums.product.ProductStatus;
import com.fjb.common.global.GlobalConfig;
import com.fjb.controller.base.BaseController;
import com.fjb.pojo.banner.BannerInfo;
import com.fjb.pojo.banner.vo.BannerInfoVo;
import com.fjb.pojo.brand.BrandInfo;
import com.fjb.pojo.brand.vo.BrandInfoVo;
import com.fjb.pojo.product.query.ProductInfoQuery;
import com.fjb.pojo.product.vo.ProductInfoPageVo;
import com.fjb.service.banner.BannerInfoService;
import com.fjb.service.brand.BrandInfoService;
import com.fjb.service.product.ProductInfoService;

/**
 * @Description:首页
 * @author hemiao
 * @time:2020年1月28日 下午7:32:30
 */
@Controller
@RequestMapping("/index")
public class IndexController extends BaseController{
	
	@Autowired
	private BrandInfoService brandInfoService;
	@Autowired
	private ProductInfoService productInfoService;
	@Autowired
	private BannerInfoService bannerInfoService;
	
	/**
	 * @Description:首页顶部录播图列表
	 * @param request
	 * @return
	 * JsonResult<Map<String,Object>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月31日 下午8:30:23
	 */
	@RequestMapping(value="/selectList")
	@ResponseBody
	public JsonResult<Map<String, Object>> selectList(HttpServletRequest request){
		HashedMap<String, Object> resultMap = new HashedMap<String, Object>();
				
		QueryWrapper<BannerInfo> queryWrapper = new QueryWrapper<BannerInfo>();
		queryWrapper.eq("banner_status", BannerStatus.STATUS_1.getCode());
		List<BannerInfo> bannerList = bannerInfoService.list(queryWrapper);
		if(bannerList.size()>0) {
			for (BannerInfo bi : bannerList) {
				String fileUrl = bi.getFileUrl();
				if(StringUtils.isNoneBlank(fileUrl)) {
					bi.setFileUrl(GlobalConfig.FILE_SERVICE_LOCATION+fileUrl);
				}
			}
		}
		// 广告列表
		resultMap.put("bannerList", bannerList);
		
		BrandInfo brandInfo = new BrandInfo();
		brandInfo.setBrandStatus(BrandStatus.STATUS_1.getCode());
		brandInfo.setHotSwitch(1);	
		List<BrandInfoVo>  brandList = brandInfoService.selectBrandList(brandInfo);
		if(brandList.size()>0) {
			for (BrandInfoVo brandInfoVo : brandList) {
				String logo = brandInfoVo.getBrandLogo();
				if(StringUtils.isNoneBlank(logo)) {
					brandInfoVo.setBrandLogo(GlobalConfig.FILE_SERVICE_LOCATION+logo);
				}
			}
		}
		// 品牌列表
		resultMap.put("brandList", brandList);
		
		PageQuery pageQuery = new PageQuery();
		pageQuery.setCurrentPage(1);
		pageQuery.setPageSize(8);
		Integer productStatus = ProductStatus.STATUS_1.getCode();
		// 新品产品列表
		ProductInfoQuery infoQuery1 = new ProductInfoQuery();
		infoQuery1.setProductStatus(productStatus);
		infoQuery1.setIsNew(1);
		TotalResult<List<ProductInfoPageVo>> isNewPageList = productInfoService.selectIndexList(infoQuery1, pageQuery);
		if(isNewPageList.getDataObject().size()>0) {
			for (ProductInfoPageVo info : isNewPageList.getDataObject()) {
				String mainFileUrl = info.getMainFileUrl();
				String videoCoverUrl = info.getVideoCoverUrl();
				if(StringUtils.isNoneBlank(mainFileUrl)) {
					info.setMainFileUrl(GlobalConfig.FILE_SERVICE_LOCATION+mainFileUrl);
				}
				if(StringUtils.isNoneBlank(videoCoverUrl)) {
					info.setVideoCoverUrl(GlobalConfig.FILE_SERVICE_LOCATION+videoCoverUrl);
				}
			}
		}
		resultMap.put("newList", isNewPageList);
		// 特卖列表
		ProductInfoQuery infoQuery2 = new ProductInfoQuery();
		infoQuery2.setProductStatus(productStatus);
		infoQuery2.setIsHot(1);		
		TotalResult<List<ProductInfoPageVo>> isHotPageList = productInfoService.selectIndexList(infoQuery2, pageQuery);
		if(isHotPageList.getDataObject().size()>0) {
			for (ProductInfoPageVo info : isHotPageList.getDataObject()) {
				String mainFileUrl = info.getMainFileUrl();
				String videoCoverUrl = info.getVideoCoverUrl();
				if(StringUtils.isNoneBlank(mainFileUrl)) {
					info.setMainFileUrl(GlobalConfig.FILE_SERVICE_LOCATION+mainFileUrl);
				}
				if(StringUtils.isNoneBlank(videoCoverUrl)) {
					info.setVideoCoverUrl(GlobalConfig.FILE_SERVICE_LOCATION+videoCoverUrl);
				}
			}
		}
		resultMap.put("hotList", isHotPageList);
		return new JsonResult<Map<String, Object>>(resultMap, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:查询banner列表
	 * @param request
	 * @return
	 * JsonResult<List<BrandInfoVo>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月30日 下午5:39:51
	 */
	@RequestMapping(value="/selectBannerList")
	@ResponseBody
	public JsonResult<List<BannerInfoVo>> selectBannerList(HttpServletRequest request){
		List<BannerInfoVo> infoList = new ArrayList<BannerInfoVo>();
		LocalDateTime now = LocalDateTime.now();
		BannerInfoVo b1 = new BannerInfoVo();
		b1.setId(1000);
		b1.setName("合作 谁是你的菜");
		b1.setLink("");
		b1.setUrl("http://yanxuan.nosdn.127.net/65091eebc48899298171c2eb6696fe27.jpg");
		b1.setContent("合作 谁是你的菜");
		b1.setPosition(1);
		b1.setAddTime(now);
		b1.setUpdateTime(now);
		b1.setEnabled(false);
		infoList.add(b1);
		
		BannerInfoVo b2 = new BannerInfoVo();
		b2.setId(1001);
		b2.setName("活动 美食节");
		b2.setLink("");
		b2.setUrl("http://yanxuan.nosdn.127.net/bff2e49136fcef1fd829f5036e07f116.jpg");
		b2.setContent("活动 美食节");
		b2.setPosition(1);
		b2.setAddTime(now);
		b2.setUpdateTime(now);
		b2.setEnabled(true);
		infoList.add(b2);
		
		BannerInfoVo b3 = new BannerInfoVo();
		b3.setId(1002);
		b3.setName("活动 母亲节");
		b3.setLink("");
		b3.setUrl("http://yanxuan.nosdn.127.net/8e50c65fda145e6dd1bf4fb7ee0fcecc.jpg");
		b3.setContent("活动 母亲节");
		b3.setPosition(1);
		b3.setAddTime(now);
		b3.setUpdateTime(now);
		b3.setEnabled(false);
		infoList.add(b3);
		
		return new JsonResult<List<BannerInfoVo>>(infoList, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:获得品牌列表
	 * @param request
	 * @return
	 * JsonResult<List<BrandInfoVo>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月29日 下午8:07:50
	 */
	@RequestMapping(value="/selectBrandList")
	@ResponseBody
	public JsonResult<List<BrandInfoVo>> selectBrandList(HttpServletRequest request){
		BrandInfo brandInfo = new BrandInfo();
		brandInfo.setBrandStatus(BrandStatus.STATUS_1.getCode());
		List<BrandInfoVo>  brandList = brandInfoService.selectBrandList(brandInfo);
		return new JsonResult<List<BrandInfoVo>>(brandList, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:首页商品列表
	 * @param request
	 * @return
	 * JsonResult<Map<String,List<ProductInfoPageVo>>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月29日 下午8:32:43
	 */
	@RequestMapping(value="/selectProductList")
	@ResponseBody
	public JsonResult<Map<String, Object>> selectProductList(HttpServletRequest request){
		Map<String, Object> resultMap = new HashedMap<String, Object>();
		PageQuery pageQuery = new PageQuery();
		pageQuery.setCurrentPage(1);
		pageQuery.setPageSize(8);
		Integer productStatus = ProductStatus.STATUS_1.getCode();
		// 新品产品列表
		ProductInfoQuery infoQuery1 = new ProductInfoQuery();
		infoQuery1.setProductStatus(productStatus);
		infoQuery1.setIsNew(1);
		JsonTotalResult<List<ProductInfoPageVo>> isNewPageList = productInfoService.selectPageList(infoQuery1, pageQuery);
		resultMap.put("newPageList", isNewPageList);
		// 特卖列表
		ProductInfoQuery infoQuery2 = new ProductInfoQuery();
		infoQuery2.setProductStatus(productStatus);
		infoQuery2.setIsHot(1);	
		JsonTotalResult<List<ProductInfoPageVo>> isHotPageList = productInfoService.selectPageList(infoQuery2, pageQuery);
		resultMap.put("hotPageList", isHotPageList);
		return new JsonResult<Map<String, Object>>(resultMap, HttpCode.SUCCESS);
	}
}
